Note
tonal-note
is a collection of functions to manipulate musical notes in scientific notation
This is part of tonal music theory library.
Usage
import * as Note from "tonal-note"
// or const Note = require("tonal-note")
Note.name("bb2") // => "Bb2"
Note.chroma("bb2") // => 10
Note.midi("a4") // => 69
Note.freq("a4") // => 440
Note.oct("G3") // => 3
// part of tonal
const Tonal = require("tonal")
// or import Note from "tonal"
Tonal.Note.midi("d4") // => 62
Install
API Documentation
Note.midi ⇒ Number
Get the frequency from midi number
Kind: static property of Note
Returns: Number
-
the frequency or null if not valid note midi
Param | Type | Description |
---|
midi | Number | the note midi number |
tuning | Number | (Optional) 440 by default |
Note.freqToMidi ⇒ Integer
Return the chroma of a note. The chroma is the numeric equivalent to the
pitch class, where 0 is C, 1 is C# or Db, 2 is D... 11 is B
Kind: static property of Note
Returns: Integer
-
the chroma number
Param | Type | Description |
---|
note | string | the note name |
Example
Note.chroma("Cb")
["C", "D", "E", "F"].map(Note.chroma)
Note.from
Deprecated. This is kept for backwards compatibility only.
Use Note.from instead
Kind: static property of Note
Note.simplify ⇒ string
Get the simplified and enhramonic note of the given one.
Kind: static property of Note
Returns: string
-
the enhramonic note
Example
Note.enharmonic("Db")
Note.enhramonic("C")
Note.props() ⇒ string
Given a note name, return the note name or null if not valid note.
The note name will ALWAYS have the letter in upercase and accidentals
using # or b
Can be used to test if a string is a valid note name.
Kind: static method of Note
Example
Note.name("cb2")
["c", "db3", "2", "g+", "gx4"].map(Note.name)
Note.name() ⇒ string
Get pitch class of a note. The note can be a string or a pitch array.
Kind: static method of Note
Returns: string
-
the pitch class
Example
Note.pc("Db3")
["db3", "bb6", "fx2"].map(Note.pc)
Note.midiToFreq(note) ⇒ Number
Get the frequency of a note
Kind: static method of Note
Returns: Number
-
the frequency
Param | Type | Description |
---|
note | string | Number | the note name or midi note number |
Example
Note.freq("A4")
Note.freq(69)
Note.chroma(note) ⇒ Integer
Get the octave of the given pitch
Kind: static method of Note
Returns: Integer
-
the octave or null if doesn"t have an octave or not a valid note
Param | Type | Description |
---|
note | string | the note |
Example
Note.oct("C#4")
Note.oct("C")
Note.oct("blah")
Note.altToAcc(props, [baseNote]) ⇒ string
Creates a note name in scientific notation from note properties,
and optionally another note name.
It receives an object with:
- step: the note step (0 = C, 1 = D, ... 6 = B)
- alt: (optional) the alteration. Negative numbers are flats, positive sharps
- oct: (optional) the octave
Optionally it receives another note as a "base", meaning that any prop not explicitly
received on the first parameter will be taken from that base note. That way it can be used
as an immutable "set" operator for a that base note
Kind: static method of Note
Returns: string
-
the note name in scientific notation or null if not valid properties
Param | Type | Description |
---|
props | Object | the note properties |
[baseNote] | string | note to build the result from. If given, it returns the result of applying the given props to this note. |
Example
Note.from({ step: 5 })
Note.from({ step: 1, acc: -1 })
Note.from({ step: 2, acc: 2, oct: 2 })
Note.from({ step: 7 })
Note.from({alt: 1, oct: 3}, "C4")
Note.build(midi, useSharps) ⇒ string
Given a midi number, returns a note name. The altered notes will have
flats unless explicitly set with the optional useSharps
parameter.
Kind: static method of Note
Returns: string
-
the note name
Param | Type | Description |
---|
midi | number | the midi note number |
useSharps | boolean | (Optional) set to true to use sharps instead of flats |
Example
Note.fromMidi(61)
Note.fromMidi(61, true)
Note.fromMidi(61.7)
Note~props ⇒ Object
Get note properties. It returns an object with the following information:
- name {string}: the note name. The letter is always in uppercase
- letter {string}: the note letter, always in uppercase
- acc {string}: the note accidentals
- octave {Number}: the octave or null if not present
- pc {string}: the pitch class (letter + accidentals)
- step {Number}: number equivalent of the note letter. 0 means C ... 6 means B.
- alt {Number}: number equivalent of accidentals (negative are flats, positive sharps)
- chroma {Number}: number equivalent of the pitch class, where 0 is C, 1 is C# or Db, 2 is D...
- midi {Number}: the note midi number (IMPORTANT! it can be outside 0 to 127 range)
- freq {Number}: the frequency using an equal temperament at 440Hz
This function always returns an object with all this properties, but if it"s
not a valid note all properties will be null.
The returned object can"t be mutated.
Kind: inner constant of Note
Returns: Object
-
an object with the properties (or an object will all properties
set to null if not valid note)
Param | Type | Description |
---|
note | string | the note name in scientific notation |
Example
Note.props("fx-3").name
Note.props("invalid").name
Note.props("C#3").oct
Note.props().oct
Note~names(accTypes) ⇒ Array
Get a list of note names (pitch classes) within a octave
Kind: inner method of Note
Param | Type | Description |
---|
accTypes | string | (Optional, by default " b#"). A string with the accidentals types: " " means no accidental, "#" means sharps, "b" mean flats, can be combined (see examples) |
Example
Note.names(" b")
Note.names(" #")
Note~tokenize(str) ⇒ Array
Split a string into tokens related to note parts.
It returns an array of strings [letter, accidental, octave, modifier]
It always returns an array
Kind: inner method of Note
Returns: Array
-
an array of note tokens
Example
Note.tokenize("C#2")
Note.tokenize("Db3 major")
Note.tokenize("major")
Note.tokenize("##")
Note.tokenize()
Note~midi(note) ⇒ Integer
Get the note midi number. It always return a number between 0 and 127
Kind: inner method of Note
Returns: Integer
-
the midi number or null if not valid pitch
See: midi.toMidi
Param | Type | Description |
---|
note | string | Number | the note to get the midi number from |
Example
Note.midi("C4")
Note.midi(60)
Note~freqToMidi(frequency) ⇒ Number
Get the midi number from a frequency in hertz. The midi number can
contain decimals (with two digits precission)
Kind: inner method of Note
Example
Note.freqToMidi(220));
Note.freqToMidi(261.62));
Note.freqToMidi(261));
Note~stepToLetter(step) ⇒ string
Given a step number return it's letter (0 = C, 1 = D, 2 = E)
Kind: inner method of Note
Returns: string
-
the letter
Example
Note.stepToLetter(3)
Note~altToAcc(alt) ⇒ string
Given an alteration number, return the accidentals
Kind: inner method of Note
Example
Note.altToAcc(-3)
Note~simplify(note, useSameAccType) ⇒ string
Simplify the note: find an enhramonic note with less accidentals.
Kind: inner method of Note
Returns: string
-
the simplfiied note or null if not valid note
Param | Type | Description |
---|
note | string | the note to be simplified |
useSameAccType | boolean | (optional, true by default) set to true to ensure the returned note has the same accidental types that the given note |
Example
Note.simplify("C##")
Note.simplify("C###")
Note.simplify("C###", false)
Note.simplify("B#4")